package com.payroll.taglib.html;

import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;
import com.payroll.framework.Foundation;

/**
 * Insert the type's description here.
 * Creation date: (1/9/02 10:35:24 AM)
 * @author: Administrator
 */
public class PayrollErrorsTag extends org.apache.struts.taglib.html.ErrorsTag {
/**
 * Override the doStartTag to get the message from exc_message
 * Creation date: (1/9/02 10:40:55 AM)
 * @return int
 * @exception javax.servlet.jsp.JspException The exception description.
 */
public int doStartTag() throws JspException {

    // Were any error messages specified?
    ActionErrors errors = new ActionErrors();
    try {
        Object value = pageContext.getAttribute(name, PageContext.REQUEST_SCOPE);
        if (value == null) {
            ;
        } else if (value instanceof String) {
        	errors.add(ActionErrors.GLOBAL_ERROR, new ActionError((String) value));
        } else if (value instanceof String[]) {
			String keys[] = (String[]) value;
			for (int i = 0; i < keys.length; i++)
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(keys[i]));
		} else if (value instanceof ActionErrors) {
			errors = (ActionErrors) value;
		} else {
			JspException e = new JspException(
				messages.getMessage("errorsTag.errors", value.getClass().getName()));
 			RequestUtils.saveException(pageContext, e);
			throw e;
		}
    } catch (Exception e) {
        ;
    }

    if (errors.isEmpty())
        return (EVAL_BODY_INCLUDE);

    // Check for presence of header and footer message keys
    //boolean headerPresent =
    //    RequestUtils.present(pageContext, bundle, locale, "errors.header");
    //boolean footerPresent =
    //    RequestUtils.present(pageContext, bundle, locale, "errors.footer");

    // Render the error messages appropriately
    StringBuffer results = new StringBuffer();
    //String message = null;
    //if (headerPresent)
    //    message = RequestUtils.message(pageContext, bundle, locale, "errors.header");
    //if (message != null) {
    //    results.append(message);
    //    results.append("\r\n");
    //}

    String header = "<DIV ID=\"divErrors\" style=\"display:none;\">";
    results.append(header);

    String message = null;

    Iterator reports = null;
    if (property == null)
        reports = errors.get();
    else
        reports = errors.get(property);
    while (reports.hasNext()) {
        ActionError report = (ActionError) reports.next();
        message = Foundation.getFormattedEXCMessage(report);
        if (message != null) {
	        results.append("<li>");
            results.append(message);
            results.append("</li>");
            results.append("\r\n");
        }
    }
    //message = null;
    //if (footerPresent)
    //    message = RequestUtils.message(pageContext, bundle, locale, "errors.footer");
    //if (message != null) {
    //    results.append(message);
    //    results.append("\r\n");
    //}
    String footer = "</DIV>";
    results.append(footer);

    // Print the results to our output writer
    ResponseUtils.write(pageContext, results.toString());

    // Continue processing this page
    return (EVAL_BODY_INCLUDE);

}
}
